package org.spring.springboot.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spring.springboot.domain.AdminMenu;
import org.spring.springboot.domain.AdminUser;
import org.spring.springboot.service.MenuService;
import org.spring.springboot.service.UserService;
import org.spring.springboot.util.MessageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * 描述：
 * <p>
 * Author: 赵新国
 * Date: 2017/9/29 15:38
 */
@RestController
@RequestMapping("/admin")
public class LoginController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MenuService menuService;

    @Autowired
    private UserService userService;

    /**
     * 登录页面
     *
     * @return
     */
    @RequestMapping(value = "/login")
    public ModelAndView login(String userName, String password) {
        AdminUser adminUser = userService.login(userName, password);
        if(adminUser != null) {
            ModelAndView mav = new ModelAndView("admin/login");
            return mav;
        }
        return null;
    }

    /**
     * 登录接口
     *
     * @param username
     * @param password
     * @return
     */
    @PostMapping(value = "/doLogin")
    public HashMap<String, Object> doLogin(Model model, String username, String password) {
        HashMap<String, Object> retMap = MessageUtils.success("登录成功");
        return retMap;
    }

    /**
     * 后台首页
     *
     * @return
     */
    @RequestMapping(value = "/index")
    public ModelAndView index(Model model) {
        ModelAndView mav = new ModelAndView("admin/index");
        List<AdminMenu> menuList = menuService.getMenuList(Short.parseShort("1"));
        mav.addObject("menuList", menuList);
        return mav;
    }

    /**
     * 退出登录
     *
     * @return
     */
    @GetMapping("/logout")
    public ModelAndView logout() {
        return new ModelAndView("redirect:/admin/login");
    }

}
